package org.crmsystem.controller.shiro;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.model.Permission;
import org.crmsystem.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 过滤链
 * @author 孙艳平
 *
 */
public class FilterChainDefinitionFactoryBean {
	/**
	 * log4j日志
	 */
	private static Logger logger=LogManager.getLogger(FilterChainDefinitionFactoryBean.class);
	/**
	 * 权限服务层注入
	 */
	@Autowired
	private IPermissionService iPermissionService;
	/**
	 * 过滤链map
	 * @return
	 */
	public Map<String, String> buildFilterChainDefinitionMap(){
		Map<String, String> map = new HashMap<>();
		map.put("/login.html", "anon");
		map.put("/toLogin", "anon");
		map.put("/refuse.html", "anon");
		map.put("/error.html", "anon");
		map.put("/static/img/**", "anon");
		map.put("/static/js/**", "anon");
		map.put("/static/css/**", "anon");
		map.put("/static/cityJSON.txt", "anon");
		map.put("/laydate/**", "anon");
		map.put("/logout", "logout");
		map.put("/**", "authc");
		List<Permission> list=null;
		try {
			Permission permission=new Permission();
			list = this.iPermissionService.getPermission(permission);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for(Permission p:list){
			if(p.getUrl()!=null && !p.getUrl().equals("")){
				if(p.getName()!=null && !p.getName().equals("")){
					map.put("authc,perms["+p.getName()+"]",p.getUrl());
				}
			}
		}
		return map;
	}
	
}
